#include "ScreenCapture.h"
#include <iostream>
#include <string>
#include <conio.h>

void PrintUsage() {
    std::cout << "ScreenCapture Test Program" << std::endl;
    std::cout << "==========================" << std::endl;
    std::cout << "Commands:" << std::endl;
    std::cout << "  s - Start continuous capture" << std::endl;
    std::cout << "  t - Stop capture" << std::endl;
    std::cout << "  c - Capture single frame" << std::endl;
    std::cout << "  i - Show capture info" << std::endl;
    std::cout << "  g - Force GDI capture" << std::endl;
    std::cout << "  d - Force DXGI capture" << std::endl;
    std::cout << "  a - Auto capture method" << std::endl;
    std::cout << "  q - Quit" << std::endl;
    std::cout << std::endl;
}

void PrintCaptureInfo(const ScreenCapture& capture) {
    std::cout << "Capture Status:" << std::endl;
    std::cout << "  Running: " << (capture.IsCapturing() ? "Yes" : "No") << std::endl;

    const char* methodName = "Unknown";
    switch (capture.GetActiveMethod()) {
        case CaptureMethod::GDI:
            methodName = "GDI";
            break;
        case CaptureMethod::DXGI:
            methodName = "DXGI";
            break;
        case CaptureMethod::AUTO:
            methodName = "Auto";
            break;
    }
    std::cout << "  Method: " << methodName << std::endl;

    std::string lastError = capture.GetLastError();
    if (!lastError.empty()) {
        std::cout << "  Last Error: " << lastError << std::endl;
    }
    std::cout << std::endl;
}

const char* CaptureResultToString(CaptureResult result) {
    switch (result) {
        case CaptureResult::SUCCESS:
            return "Success";
        case CaptureResult::ERROR_INIT_FAILED:
            return "Initialization Failed";
        case CaptureResult::ERROR_CAPTURE_FAILED:
            return "Capture Failed";
        case CaptureResult::ERROR_SAVE_FAILED:
            return "Save Failed";
        case CaptureResult::ERROR_DESKTOP_SWITCH:
            return "Desktop Switch Error";
        default:
            return "Unknown Error";
    }
}

int main() {
    std::cout << "Windows Screen Capture Library Test" << std::endl;
    std::cout << "===================================" << std::endl;
    std::cout << std::endl;

    ScreenCapture capture;
    CaptureSettings settings;

    settings.method = CaptureMethod::AUTO;
    settings.outputPath = "C:\\ScreenCapture";
    settings.captureInterval = 2000;
    settings.enableDesktopMonitoring = true;
    settings.captureSecureDesktops = true;

    std::cout << "Initializing screen capture..." << std::endl;
    CaptureResult result = capture.Initialize(settings);

    if (result != CaptureResult::SUCCESS) {
        std::cout << "Failed to initialize: " << CaptureResultToString(result) << std::endl;
        std::cout << "Error: " << capture.GetLastError() << std::endl;
        std::cout << "Press any key to exit..." << std::endl;
        _getch();
        return 1;
    }

    std::cout << "Initialization successful!" << std::endl;
    std::cout << "Output directory: " << settings.outputPath << std::endl;
    std::cout << "Capture interval: " << settings.captureInterval << "ms" << std::endl;
    std::cout << std::endl;

    PrintUsage();
    PrintCaptureInfo(capture);

    bool running = true;
    while (running) {
        std::cout << "Command: ";
        char cmd = _getch();
        std::cout << cmd << std::endl;

        switch (cmd) {
            case 's':
            case 'S':
                std::cout << "Starting continuous capture..." << std::endl;
                result = capture.StartCapture();
                if (result == CaptureResult::SUCCESS) {
                    std::cout << "Capture started successfully!" << std::endl;
                } else {
                    std::cout << "Failed to start capture: " << CaptureResultToString(result) << std::endl;
                    std::cout << "Error: " << capture.GetLastError() << std::endl;
                }
                break;

            case 't':
            case 'T':
                std::cout << "Stopping capture..." << std::endl;
                result = capture.StopCapture();
                if (result == CaptureResult::SUCCESS) {
                    std::cout << "Capture stopped successfully!" << std::endl;
                } else {
                    std::cout << "Failed to stop capture: " << CaptureResultToString(result) << std::endl;
                }
                break;

            case 'c':
            case 'C':
                std::cout << "Capturing single frame..." << std::endl;
                result = capture.CaptureFrame();
                if (result == CaptureResult::SUCCESS) {
                    std::cout << "Frame captured successfully!" << std::endl;
                } else {
                    std::cout << "Failed to capture frame: " << CaptureResultToString(result) << std::endl;
                    std::cout << "Error: " << capture.GetLastError() << std::endl;
                }
                break;

            case 'i':
            case 'I':
                PrintCaptureInfo(capture);
                break;

            case 'g':
            case 'G':
                if (capture.IsCapturing()) {
                    std::cout << "Stop capture first before changing method!" << std::endl;
                } else {
                    std::cout << "Switching to GDI capture method..." << std::endl;
                    settings.method = CaptureMethod::GDI;
                    result = capture.Initialize(settings);
                    if (result == CaptureResult::SUCCESS) {
                        std::cout << "Switched to GDI method successfully!" << std::endl;
                    } else {
                        std::cout << "Failed to switch to GDI: " << CaptureResultToString(result) << std::endl;
                    }
                }
                break;

            case 'd':
            case 'D':
                if (capture.IsCapturing()) {
                    std::cout << "Stop capture first before changing method!" << std::endl;
                } else {
                    std::cout << "Switching to DXGI capture method..." << std::endl;
                    settings.method = CaptureMethod::DXGI;
                    result = capture.Initialize(settings);
                    if (result == CaptureResult::SUCCESS) {
                        std::cout << "Switched to DXGI method successfully!" << std::endl;
                    } else {
                        std::cout << "Failed to switch to DXGI: " << CaptureResultToString(result) << std::endl;
                        std::cout << "Error: " << capture.GetLastError() << std::endl;
                    }
                }
                break;

            case 'a':
            case 'A':
                if (capture.IsCapturing()) {
                    std::cout << "Stop capture first before changing method!" << std::endl;
                } else {
                    std::cout << "Switching to AUTO capture method..." << std::endl;
                    settings.method = CaptureMethod::AUTO;
                    result = capture.Initialize(settings);
                    if (result == CaptureResult::SUCCESS) {
                        std::cout << "Switched to AUTO method successfully!" << std::endl;
                    } else {
                        std::cout << "Failed to switch to AUTO: " << CaptureResultToString(result) << std::endl;
                    }
                }
                break;

            case 'q':
            case 'Q':
                std::cout << "Exiting..." << std::endl;
                running = false;
                break;

            default:
                std::cout << "Unknown command. Available commands:" << std::endl;
                PrintUsage();
                break;
        }

        std::cout << std::endl;
    }

    if (capture.IsCapturing()) {
        std::cout << "Stopping capture before exit..." << std::endl;
        capture.StopCapture();
    }

    std::cout << "Goodbye!" << std::endl;
    return 0;
}